# !/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time
from multiprocessing import Process, Queue


def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)
        time.sleep(1)


def read(q):
    print('Process to read: %s' % os.getpid())
    while True:
        # if q.empty():
        #     return
        value = q.get(True)
        print('Get %s from queue.' % value)


if __name__ == "__main__":
    print("main start")
    queue = Queue()
    pw = Process(target=write, args=(queue,))
    pr = Process(target=read, args=(queue,))

    pw.start()
    pr.start()

    pw.join()  # 等待pw结束
    pr.join()
    # pr.terminate()  # pr进程里是死循环，无法等待其结束，只能强行终止
    print("main end")


